
package com.lmuse.xmr.foodspotting.ui.userpage;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;

public class SettingActivity extends AppCompatActivity implements Contract.UserView{

    private ListView settingLv;
    private ArrayAdapter<String> arrayAdapter;
    private AlertDialog nickDialog;
    private AlertDialog pwdDialog;
    private AlertDialog sendEmailDialog;
    private Button logout;
    private EditText nickEt;
    private EditText oldPwdEt;
    private EditText newPwdEt;

    private Contract.UserPresenter userPresenter;

    private List<String> settingData;
    private String oldPwd;
    private String newPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        userPresenter = new UserPresenterImpl(this);
        initView();
    }

    void initView() {
        getData();
        settingLv = (ListView) findViewById(R.id.lv_setting);
        logout = (Button) findViewById(R.id.btn_logout);
        settingLv.setOnItemClickListener(new ItemClickListener());
        logout.setOnClickListener(new ButtonClickListener());
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, settingData);
        settingLv.setAdapter(arrayAdapter);

        nickEt = new EditText(this);

        nickDialog = new AlertDialog
                .Builder(this)
                .setTitle("请输入昵称")
                .setView(nickEt)
                .setPositiveButton("确定", new NickButtonClickListener())
                .setNegativeButton("取消", null)
                .create();

        pwdDialog = new AlertDialog
                .Builder(this)
                .create();

        sendEmailDialog = new AlertDialog
                .Builder(this)
                .setTitle("是否重新发送验证邮件？")
                .setPositiveButton("确定", new SendEmailButtonClickListener())
                .setNegativeButton("取消", null)
                .create();
    }

    void getData() {
        settingData = new ArrayList<String>();
        settingData.add("修改昵称");
        settingData.add("修改密码");
        settingData.add("重新发送验证邮件");
        settingData.add("");
    }

    class ItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
                case 0:
                    nickDialog.show();
                    break;
                case 1:
                    showPwdDialog();
                    break;
                case 2:
                    sendEmailDialog.show();
                    break;
            }
        }
    }

    class NickButtonClickListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (nickEt.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "昵称不能为空!", Toast.LENGTH_LONG).show();
            } else {
                userPresenter.updateNickname(nickEt.getText().toString());
            }
        }
    }

    void showPwdDialog() {

        pwdDialog.show();
        Window window = pwdDialog.getWindow();
        window.setContentView(R.layout.modify_pwd_dialog);
        //弹出软键盘
        pwdDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        oldPwdEt = (EditText) window.findViewById(R.id.et_input_old_pwd);
        newPwdEt = (EditText) window.findViewById(R.id.et_input_new_pwd);

        ((Button)window.findViewById(R.id.btn_dialog_positive)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                oldPwd = oldPwdEt.getText().toString();
                newPwd = newPwdEt.getText().toString();

                //如果输入的新密码不为空
                if(!newPwd.equals("")){
                    userPresenter.updatePassword(oldPwd, newPwd);
                }else{
                    Toast.makeText(getApplicationContext(), "新密码不能为空!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    class SendEmailButtonClickListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            userPresenter.requestEmailVerify();
        }
    }

    class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_logout:
                    userPresenter.logOut();
                    SettingActivity.this.finish();
                    break;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        userPresenter.onDetach();
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {

    }

    @Override
    public void onSignInFailed(String msg) {

    }

    @Override
    public void onUserInfoCache(User user) {

    }

    @Override
    public void onUpdateUserSuccess() {
        Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
        if (nickDialog != null) {
            nickDialog.dismiss();
        }
        if (pwdDialog != null) {
            pwdDialog.dismiss();
        }
    }

    @Override
    public void onUpdateUserFailed(String msg) {
        Toast.makeText(this, "修改失败(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {
        if (e == null) {
            Toast.makeText(this, "发送成功！", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {

    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {

    }

    @Override
    public void onQueryUserAmountSuccess(int size) {

    }

    @Override
    public void onQueryUserAmountFailed(String msg) {

    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {

    }

    @Override
    public void onQueryByKeyFailed(String msg) {

    }
}
